$(function(){
	//datagrid 分页
	//var pager = $('#dg').datagrid().datagrid('getPager');	// get the pager of datagrid

    loadUserList();


	//highcharts
	$('#container').highcharts({
        title: {
            text: 'Monthly Average Temperature',
            x: -20 //center
        },
        subtitle: {
            text: 'Source: WorldClimate.com',
            x: -20
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        yAxis: {
            title: {
                text: 'Temperature (°C)'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            valueSuffix: '°C'
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'middle',
            borderWidth: 0
        },
        series: [{
            name: 'Tokyo',
            data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
        }, {
            name: 'New York',
            data: [-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5]
        }, {
            name: 'Berlin',
            data: [-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0]
        }, {
            name: 'London',
            data: [3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
        }]
    });
});

//treeGrid
function formatProgress(value){
    if (value){
        var s = '<div style="width:100%;border:1px solid #ccc">' +
                '<div style="width:' + value + '%;background:#cc0000;color:#fff">' + value + '%' + '</div>'
                '</div>';
        return s;
    } else {
        return '';
    }
}
var editingId;
function edit(){
    if (editingId != undefined){
        $('#tg').treegrid('select', editingId);
        return;
    }
    var row = $('#tg').treegrid('getSelected');
    if (row){
        editingId = row.id
        $('#tg').treegrid('beginEdit', editingId);
    }
}

function save(){
    if (editingId != undefined){
        var t = $('#tg');
        t.treegrid('endEdit', editingId);
        editingId = undefined;
        var persons = 0;
        var rows = t.treegrid('getChildren');
        for(var i=0; i<rows.length; i++){
            var p = parseInt(rows[i].persons);
            if (!isNaN(p)){
                persons += p;
            }
        }
        var frow = t.treegrid('getFooterRows')[0];
        frow.persons = persons;
        t.treegrid('reloadFooter');
    }
}
function cancel(){
    if (editingId != undefined){
        $('#tg').treegrid('cancelEdit', editingId);
        editingId = undefined;
    }
}

/*用户管理*/
function loadUserList(){
    $("#dg").datagrid({
        url: "userList.do",
        loadMsg: '正在加载信息...',
        width: "100%",
        idField: "Id",
        fitColumns: true,
        pagination: true,
        pageSize: 10,
        pageList: [10, 20, 35, 50],
        singleSelect: false,
        rownumbers: true,
        columns: [[
            { field: 'ck', checkbox: true, align: 'left', width: 50 },
            { field: 'nickname', title: '昵称', width: 120 },
            { field: 'deptId', title: '所属部门', width: 80 },
            { field: 'mobile', title: '联系电话', width: 120 },
            { field: 'sBirthday', title: '出生日期', width: 120 },
            { field: 'sCreateTime', title: '创建时间', width: 120 }

        ]],
        onLoadSuccess: function (data) {
            if (!data.rows) {
                var body = $(this).data().datagrid.dc.body2;
                body.find('table tbody').append('<tr><td width="' + body.width() + '" style="height: 25px; text-align: center;">没有数据</td></tr>');
            }
        }
    });
}
function addUser(){
    console.info("addUser");
    $('#addUserDia').dialog('open');
}

function delUser(){
    console.info("del user");
}

function editUser(){
    console.info("editUser");
    var rows = $('#dg').datagrid('getSelections');
    if(rows.length == 1){
        var userId = rows[0].id;
        $('#editUserForm').form("load","editUser.do?userId=" + userId);
        $('#editUserDia').dialog('open');
    }else{
        $.messager.alert('编辑用户','请选择一名用户进行编辑！');
    }
}

function cancelEdit(){
    $('#editUserDia').dialog('close');
}

function searchUser(){
    console.info("search user");
}

function submitAddForm(){
    $('#addUserForm').form("submit",{
        url : 'addUser.do',
        onSubmit:function(){
            return $(this).form('enableValidation').form('validate');
        },
        success : function(result) {
            if(result.result = "success"){
                $.messager.alert('添加用户','添加用户成功');
                $('#addUserForm').form('clear');
                $('#addUserDia').dialog('close');
                loadUserList();
            }else{
                alert("添加失败");
            }
        }
    });
}

function submitEditForm(){
    $('#editUserForm').form("submit",{
        url : 'addUser.do',
        onSubmit:function(){
            return $(this).form('enableValidation').form('validate');
        },
        success : function(result) {
            if(result.result = "success"){
                $.messager.alert('编辑用户','编辑用户成功');
                $('#editUserForm').form('clear');
                $('#editUserDia').dialog('close');
                $('#dg').datagrid('clearChecked');
                $('#dg').datagrid("reload");
            }else{
                alert("保存失败");
            }
        }
    });
}

function clearForm(){
    $('#addUserForm').form('clear');
}

function getSelections(){
    var arr = [];
    var rows = $('#dg').datagrid('getSelections');
    for(var i=0; i<rows.length; i++){
        var row = rows[i];
        arr.push('<span>'+row.id+'</span>');
    }
    return ss;
}

function addTab(subtitle,url){
    console.info("addTab");
    if(!$('#tabs').tabs('exists',subtitle)){
        $('#tabs').tabs('add',{
            title:subtitle,
            content:createFrame(url),
            closable:true
        });
    }else{
        $('#tabs').tabs('select',subtitle);
        //$('#mm-tabupdate').click();
    }
}